//index.js
//获取应用实例
const app = getApp()
const Api = require("../../utils/api.js").API
const Store = require("../../utils/store.js").store
const codeZ = require("../../utils/util.js")
Page({
    data: {
        tabsArray: [],
        list: [],
        page: 1,
        scrollHeight: '0',
        total: 0,
        activeKey: 0,
        spuNo:"",
        spuList:[],
        select:0,
        goodsName:''
    },
    onLoad() {
        let that = this
        var ystmInfo = wx.getSystemInfoSync()
        console.log(ystmInfo)
        let clientHeight = ystmInfo.windowHeight;
        // 获取可使用窗口高度
        let clientWidth = ystmInfo.windowWidth;
        // 算出比例
        let ratio = 750 / clientWidth;
        // 算出高度(单位rpx)
        let height = clientHeight * ratio
        console.log(height)


        that.setData({
            scrollHeight: height - 120 - 10
        })

    },

    onHide: function() {

        app.globalData.cheakType = {};
    },
    onShow: function() {

        var that = this
        this.setData({

            list: []

        })
        Api.getSpusApi().then(res => {

            if (res.code == 0) {
                var resData = res.data
               
                 resData.forEach(item=>{
                     var a = item.subSpus;
                     
                     a.unshift({
                         
                         			"spuNo": item.spuNo,
                         			"spuName": "全部",
                         			"parentId": "7092222542",
                         			"subSpus": null
                         }
                         
                     )
                     })
                     resData.unshift({
                     "spuNo": " ",
                     "spuName": "全部",
                     "parentId": "0",
                     "subSpus": []})
                 

                if (resData) {
                    let data;
                    if (this.data.spuNo != "") {
                        that.setData({
                            tabsArray: resData,
                        })
                        data = {
                            "asc": true,
                            "limit": 5,
                            "page": 1,
                            "sidx": "",
                            "spuNo": this.data.spuNo
                        }
                    } else {
                        that.setData({
                            tabsArray: resData,
                            spuList:resData[0].subSpus

                        })
                        data = {
                            "asc": true,
                            "limit": 5,
                            "page": 1,
                            "sidx": "",
                            "spuNo": resData[0].spuNo,
                        }
                    }



                    that.getShopList(data, '1');
                }
            }
        })


    },
    onChange(event) {
        console.log(event)
        let that = this;
        let i = event.detail;
        let data = {
            "asc": true,
            "limit": 5,
            "page": 1,
            "sidx": "",
            "spuNo": this.data.tabsArray[i].spuNo,
        }
        this.setData({
            spuNo: this.data.tabsArray[i].spuNo,
            spuList:this.data.tabsArray[i].subSpus,
            page: 1,
            total: 0,
             select:0,
            list: []
        })
        this.getShopList(data, '2');
    },
    clickSelect:function(e){
      
        var spuNo = e.currentTarget.dataset.item.spuNo;
         var index = e.currentTarget.dataset.id;
        let data = {
            "asc": true,
            "limit": 5,
            "page": 1,
            "sidx": "",
            "spuNo": spuNo,
        }
        this.setData({
            spuNo: spuNo,
            select:index,
            page: 1,
            total: 0,
            list: []
        })
        this.getShopList(data, '2'); 
    },
    getShopList: function(data, type) {
        
        let that = this;
        Api.getGoodsListApi(data).then(res => {
            if (res.code == 0) {
                let data = res.data.records;
                let da = wx.getStorageSync('code');
               

                if (data.length == 0) {
                    return;
                }

                data.forEach(ite => {
                    if (ite.checkedNum == null) {
                        ite.checkedNum = 0;
                    } else {

                    }

                    da.forEach(function(item) {
                        if (item.value == ite.saleSpecUnit) {
                            ite.saleSpecUnit = item.name;

                        }
                        if (item.value == ite.checkoutSpecUnit) {
                            ite.checkoutSpecUnit = item.name;
                        
                        }
                        if (item.value == ite.specUnit) {
                            ite.specUnit = item.name;
                        
                        }
                        if (item.value == ite.conversionUnit) {
                            ite.conversionUnit = item.name;
                        
                        }
                    })
                })
                var NewList = that.data.list.concat(data);

                that.setData({
                    list: NewList,
                    total: res.data.total
                })
            }
        })
    },
    refreshBottom: function() {
        var that = this
        var a = this.data.page;
        var listArr = this.data.list.length;
        if (listArr == this.data.total) {
            this.setData({
                isShow: true
            })
            return
        }
        a++;
        that.setData({
            page: a
        })
        let data = {
            "asc": true,
            "limit": 5,
            "page": a,
            "sidx": "",
            "spuNo": this.data.spuNo,
        }

        this.getShopList(data, '2');


    },
    addCart: function(e) {
        var goodInfo = e.target.dataset.item;
        var i = e.target.dataset.id;
        console.log(goodInfo)
        let checkedNum = `list[${i}].checkedNum`;
       app.globalData.sum = app.globalData.sum+1;
       
       codeZ.cart_badge(app.globalData.sum);
        this.setData({
            [checkedNum]:goodInfo.checkedNum+1
            })       
        var data = {
             goodsNo:goodInfo.goodsNo,
             skuNo: goodInfo.skuNo,
             id:'',
             skuNum:goodInfo.checkedNum+1      
        }
        Api.saveCartApi(data).then(res=>{
            
            
        })
       
    
     },
     jianCart: function(e) {
         var goodInfo = e.target.dataset.item;
         var i = e.target.dataset.id;
         console.log(goodInfo)
         let checkedNum = `list[${i}].checkedNum`;
         app.globalData.sum = app.globalData.sum-1;
         codeZ.cart_badge( app.globalData.sum);
         this.setData({
             [checkedNum]:goodInfo.checkedNum-1
             })
         // ;
         var data = {
              goodsNo:goodInfo.goodsNo,
              skuNo: goodInfo.skuNo,
              id:'',
              skuNum:goodInfo.checkedNum-1               
               
         }
         Api.saveCartApi(data).then(res=>{
             
             
         })           
      },
      onSerach(e) {
         
         
          this.setData({
              goodsName: e.detail
          })
      },
        
      onSureSearch() {
          
          this.setData({
              list: [],
                         
          })
         
         let data = {
             "asc": true,
             "limit": 5,
             "page": 1,
             "sidx": "",
             "spuNo":this.data.spuNo,
             goodsName:this.data.goodsName
         }
         
         this.getShopList(data, '1');
         
      },
});
